package com.block.risk.service.type.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.risk.model.result.RiskJudgeResult;
import com.block.risk.service.IDefaultItemService;
import org.springframework.stereotype.Service;

@Service
public class RuiFuTeItemService implements IDefaultItemService {

    @Override
    public RiskJudgeResult getControlResult(JSONObject resultDetial) {

        /**
         * 650分以上
         * 公积金，企业主，房车，四选一
         *
         * 非白户，
         * 3个月笔数大于1
         * 结清大于3
         * 1个月履约2
         *
         * 无当前逾期
         * 6个月内最大逾期笔数小于3
         */

        RiskJudgeResult riskJudgeResult = new RiskJudgeResult();
        riskJudgeResult.setPass(false);

        JSONObject currentReportDetail = resultDetial.getJSONObject("current_report_detail");
        JSONObject behaviorReportDetail = resultDetial.getJSONObject("behavior_report_detail");
        JSONObject applyReportDetail = resultDetial.getJSONObject("apply_report_detail");

        if (ObjectUtil.isEmpty(behaviorReportDetail)){
            riskJudgeResult.setResult("行为数据结果为空；【behavior_report_detail】");
            return riskJudgeResult;
        }

//        // 近6个月逾期M0 = 0
        String numberSixM0 = behaviorReportDetail.getString("B22170025");
        if (Integer.valueOf(numberSixM0) > 0){
            riskJudgeResult.setResult(StrUtil.format("近6个月逾期M0不通过；【{}】", numberSixM0));
            return riskJudgeResult;
        }

        // 贷款已结清订单数
        String numberSettle = behaviorReportDetail.getString("B22170052");
        if (Integer.valueOf(numberSettle) < 3){
            riskJudgeResult.setResult(StrUtil.format("订单结清笔数3笔以上不通过；【{}】", numberSettle));
            return riskJudgeResult;
        }

        // 近3个月贷款笔数
        String B22170003 = behaviorReportDetail.getString("B22170003");
        if (Integer.valueOf(B22170003) <= 1){
            riskJudgeResult.setResult(StrUtil.format("近3个月贷款笔数1笔以上不通过；【{}】", B22170003));
            return riskJudgeResult;
        }

        // B22170045 近1 个月履约贷 款次数
        String repayNumber = behaviorReportDetail.getString("B22170045");
        if (Integer.valueOf(repayNumber) < 2){
            riskJudgeResult.setResult(StrUtil.format("近1 个月履约贷 款次数2笔以上不通过；【{}】", repayNumber));
            return riskJudgeResult;
        }

//
//        // B22170045 近1 个月履约贷 款次数
//        String repayNumber = behaviorReportDetail.getString("B22170045");
//        if (Integer.valueOf(repayNumber) < 2){
//            riskJudgeResult.setResult(StrUtil.format("近1 个月履约贷 款次数2笔以上不通过；【{}】", repayNumber));
//            return riskJudgeResult;
//        }
//
//        // 近1个月货款总金额 B22170007
//        String loanOneAmount = behaviorReportDetail.getString("B22170007");
//        if (valueListSelf.contains(loanOneAmount)){
//            riskJudgeResult.setResult(StrUtil.format("近1个月货款总金额>1000不通过；【{}】", loanOneAmount));
//            return riskJudgeResult;
//        }
//
//        // 近1 个月贷款机构数 B22170016
//        String loanJiGouNumber = behaviorReportDetail.getString("B22170016");
//        if (Integer.valueOf(loanJiGouNumber) < 1){
//            riskJudgeResult.setResult(StrUtil.format("近1 个月贷款机构数1个以上不通过；【{}】", loanJiGouNumber));
//            return riskJudgeResult;
//        }

        riskJudgeResult.setPass(true);
        riskJudgeResult.setResult("全景分控通过");
        return riskJudgeResult;
    }
}
